package com.ll.wechat.base.fragment;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ll.wechat.R;
import com.ll.wechat.chatRecord.fragment.ChatListFragment;
import com.ll.wechat.find.fragment.FindFragment;
import com.ll.wechat.friends.fragment.FriendsListFragment;
import com.ll.wechat.user.fragment.UserFragment;

/**
 * 主页Fragment
 * 
 * @ClassName: MainFragment
 * @author yeliangliang
 * @date 2015-7-28 下午5:23:09
 */
public class MainFragment extends BaseFragment implements OnClickListener {
	private ViewPager viewPager;
	/*
	 * 主页的tab选项卡 因为图片大小的问题，只能采用这种比较麻烦的方式来设计TAB栏了。
	 */
	private TextView tv_tab_1;
	private TextView tv_tab_2;
	private TextView tv_tab_3;
	private TextView tv_tab_4;
	private ImageView img_tab_1;
	private ImageView img_tab_2;
	private ImageView img_tab_3;
	private ImageView img_tab_4;
	private LinearLayout ll_tab_1;
	private LinearLayout ll_tab_2;
	private LinearLayout ll_tab_3;
	private LinearLayout ll_tab_4;
	// ViewPager的适配器
	private MyViewPagerAdapter mAdapter;
	private List<Fragment> fragmentList;
	private ChatListFragment fragment1;
	private FriendsListFragment fragment2;
	private FindFragment fragment3;
	private UserFragment fragment4;

	@Override
	public int setRootViewId() {
		return R.layout.fragment_main;
	}

	@Override
	protected void initView(View rootView) {
		// findViewById And setOnClickLinstener
		initViewId(rootView);
		// Fragment
		initFragment();
		// ViewPager
		mAdapter = new MyViewPagerAdapter(getActivity().getSupportFragmentManager());
		viewPager.setAdapter(mAdapter);
		viewPager.setOnPageChangeListener(viewPagerChangeListener);
	}

	/**
	 * 初始化Fragment界面
	 * 
	 * @author yeliangliang
	 * @date 2015-7-27 上午10:14:22
	 * @version V1.0
	 * @return void
	 */
	private void initFragment() {
		fragmentList = new ArrayList<Fragment>();
		fragment1 = new ChatListFragment();
		fragment2 = new FriendsListFragment();
		fragment3 = new FindFragment();
		fragment4 = new UserFragment();
		fragmentList.add(fragment1);
		fragmentList.add(fragment2);
		fragmentList.add(fragment3);
		fragmentList.add(fragment4);
	}

	@Override
	protected void initData() {

	}

	/**
	 * 
	 * @author yeliangliang
	 * @date 2015-7-25 下午5:27:21
	 * @version V1.0
	 * @return void
	 */
	private void initViewId(View rootView) {
		viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
		tv_tab_1 = (TextView) rootView.findViewById(R.id.tv_main_tab_1);
		tv_tab_2 = (TextView) rootView.findViewById(R.id.tv_main_tab_2);
		tv_tab_3 = (TextView) rootView.findViewById(R.id.tv_main_tab_3);
		tv_tab_4 = (TextView) rootView.findViewById(R.id.tv_main_tab_4);
		img_tab_1 = (ImageView) rootView.findViewById(R.id.img_main_tab_1);
		img_tab_2 = (ImageView) rootView.findViewById(R.id.img_main_tab_2);
		img_tab_3 = (ImageView) rootView.findViewById(R.id.img_main_tab_3);
		img_tab_4 = (ImageView) rootView.findViewById(R.id.img_main_tab_4);
		ll_tab_1 = (LinearLayout) rootView.findViewById(R.id.ll_main_tab_1);
		ll_tab_2 = (LinearLayout) rootView.findViewById(R.id.ll_main_tab_2);
		ll_tab_3 = (LinearLayout) rootView.findViewById(R.id.ll_main_tab_3);
		ll_tab_4 = (LinearLayout) rootView.findViewById(R.id.ll_main_tab_4);
		ll_tab_1.setOnClickListener(this);
		ll_tab_2.setOnClickListener(this);
		ll_tab_3.setOnClickListener(this);
		ll_tab_4.setOnClickListener(this);
		// Tab栏第一项的选中效果
		viewPagerChangeListener.onPageSelected(0);
		// 设置缓存页数（防止每次切换到第一页都重新请求好友列表）
		viewPager.setOffscreenPageLimit(4);
	}

	/**
	 * Viewpager的适配器
	 * 
	 * @ClassName: MyViewPagerAdapter
	 * @author yeliangliang
	 * @date 2015-7-25 下午6:35:25
	 */
	private class MyViewPagerAdapter extends FragmentPagerAdapter {

		public MyViewPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			return fragmentList == null || fragmentList.size() == 0 ? null : fragmentList.get(arg0);
		}

		@Override
		public int getCount() {
			return fragmentList == null || fragmentList.size() == 0 ? 0 : fragmentList.size();
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// 主页tab栏按钮点击的时候切换效果
		case R.id.ll_main_tab_1:
			viewPagerChangeListener.onPageSelected(0);
			viewPager.setCurrentItem(0, true);
			break;
		case R.id.ll_main_tab_2:
			viewPagerChangeListener.onPageSelected(1);
			viewPager.setCurrentItem(1, true);
			break;
		case R.id.ll_main_tab_3:
			viewPagerChangeListener.onPageSelected(2);
			viewPager.setCurrentItem(2, true);
			break;
		case R.id.ll_main_tab_4:
			viewPagerChangeListener.onPageSelected(3);
			viewPager.setCurrentItem(3, true);
			break;

		default:
			break;
		}
	}

	/**
	 * ViewPager的页面改变监听
	 */
	private OnPageChangeListener viewPagerChangeListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {
			// 页面被选中的时候
			// Tab选项卡的颜色改变
			// 重置颜色
			resetTabBg();
			switch (arg0) {
			case 0:// 第一个页面
					// 改变当前颜色
				img_tab_1.setImageResource(R.drawable.icon_main_tab_1_p);
				tv_tab_1.setTextColor(getResources().getColor(R.color.green));
				break;
			case 1:
				img_tab_2.setImageResource(R.drawable.icon_main_tab_2_p);
				tv_tab_2.setTextColor(getResources().getColor(R.color.green));
				break;
			case 2:
				img_tab_3.setImageResource(R.drawable.icon_main_tab_3_p);
				tv_tab_3.setTextColor(getResources().getColor(R.color.green));
				break;
			case 3:
				img_tab_4.setImageResource(R.drawable.icon_main_tab_4_p);
				tv_tab_4.setTextColor(getResources().getColor(R.color.green));
				break;

			default:
				break;
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// 页面滑动的时候
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// 页面滑动状态改变的时候
			if (viewPager.getCurrentItem() == 1) {
				fragment2.hideABC(arg0);
			}
		}
	};

	/**
	 * 重置主页Tab栏的Button背景和字体颜色效果
	 * 
	 * @param i
	 * @author yeliangliang
	 * @date 2015-7-25 下午6:53:54
	 * @version V1.0
	 * @return void
	 */
	private void resetTabBg() {
		img_tab_1.setImageResource(R.drawable.icon_main_tab_1_n);
		img_tab_2.setImageResource(R.drawable.icon_main_tab_2_n);
		img_tab_3.setImageResource(R.drawable.icon_main_tab_3_n);
		img_tab_4.setImageResource(R.drawable.icon_main_tab_4_n);
		tv_tab_1.setTextColor(R.color.gray_black);
		tv_tab_2.setTextColor(R.color.gray_black);
		tv_tab_3.setTextColor(R.color.gray_black);
		tv_tab_4.setTextColor(R.color.gray_black);
	}

}
